package cjp.itcast.service.impl;

import cjp.itcast.utils.JwtHelper;
import cjp.itcast.utils.MD5Util;
import cjp.itcast.utils.Result;
import cjp.itcast.utils.ResultCodeEnum;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cjp.itcast.pojo.User;
import cjp.itcast.service.UserService;
import cjp.itcast.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author HW
 * @description 针对表【news_user】的数据库操作Service实现
 * @createDate 2024-11-14 12:19:05
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private JwtHelper jwtHelper;

    /**
     * 登陆业务
     * <p>
     * 1。根据账号，查询用户对象-LoginUser
     * 2.如果用户对象为空，查询失败，账号错误，501
     * 3.对比密码
     * 4.失败503
     * 5.成功。根据用户id生成token--》result返回
     *
     * @param user
     * @return
     */
    @Override
    public Result login(User user) {
        //1.根据账号，查询数据-LoginUser
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUsername, user.getUsername());
        User loginUser = userMapper.selectOne(lambdaQueryWrapper);

        //2.如果用户对象为空，查询失败，账号错误，501
        if (loginUser == null) {
            return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
        }

        //3. 对比密码
        if (!StringUtils.isEmpty(user.getUserPwd()) &&
                MD5Util.encrypt(user.getUserPwd()).equals(loginUser.getUserPwd())) {
            //登陆成功
            //根据用户id生成token--》result返回
            String token = jwtHelper.createToken(Long.valueOf(loginUser.getUid()));
            Map data = new HashMap();
            data.put("token", token);
            return Result.ok(data);
        } else {
            //密码错误

            return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);

        }
    }

    /**
     * 根据token获取用户数据
     * 1.token 释是否在有效期
     * 2.根据token解析userId
     * 3.根据id查询用户数据
     * 4.去掉密码，封装result结果返回
     *
     * @param token
     * @return
     */
    @Override
    public Result getUserInfo(String token) {
        boolean expiration = jwtHelper.isExpiration(token);
        // 1.token 释是否在有效期
        if (expiration) {
            //过期,失效
            return Result.build(null, ResultCodeEnum.NOTLOGIN);
        }
        //2.根据token解析userId
        int userId = jwtHelper.getUserId(token).intValue();
        //3.根据id查询用户数据
        User user = userMapper.selectById(userId);
        //4.去掉密码，封装result结果返回
        user.setUserPwd("");
        Map data = new HashMap();
        data.put("loginUser", user);

        return Result.ok(data);
    }

    /**
     * 用户名检查
     * 1.根据账号进行count查询
     * 2.count==0可用
     * 3.count》0 不可用
     *
     * @return
     */
    @Override
    public Result checkUserName(String username) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, username);
        Long count = userMapper.selectCount(queryWrapper);
        if (count==0) {
            return Result.ok(null);
        }
        return Result.build(null,ResultCodeEnum.USERNAME_USED);
    }

    /**
     * 注册业务
     * 1.检查账号是否被注册
     * 2.密码加密
     * 3.将账号保存
     * @param user
     * @return
     */
    @Override
    public Result regist(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,user.getUsername());
        Long count = userMapper.selectCount(queryWrapper);
        if (count>0) {
            return Result.build(null,ResultCodeEnum.USERNAME_USED);
        }
        user.setUserPwd(MD5Util.encrypt(user.getUserPwd()));
        userMapper.insert(user);
        return Result.ok(null);
    }
}




